﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class CityMaster : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                clsCity clsCity = new clsCity();
                FillCity();
                FillddlState();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }

    protected void FillddlState()
    {
        try
        {
            clsCity objclsCity = new clsCity();
            List<IN_TB_STATE> objIN_TB_STATE = new List<IN_TB_STATE>();
            objIN_TB_STATE = objclsCity.GetAllStates();
            ddlState.Items.Clear();
            ddlState.AppendDataBoundItems = true;
            ddlState.DataTextField = "STATE";
            ddlState.DataValueField = "ID";
            ddlState.Items.Add("Select");
            ddlState.Items[0].Value = "0";
            ddlState.DataSource = objIN_TB_STATE;
            ddlState.DataBind();

        }
        catch (Exception)
        {

            throw;
        }
    }

    protected void btncity_Click(object sender, EventArgs e)
    {
        try
        {
            if (Session["CityId"] == null)
            {
                clsCity objclsCity = new clsCity();
                IN_TB_CITY objIN_TB_CITY = new IN_TB_CITY();
                objIN_TB_CITY.CITY = txtcityname.Text;
                objIN_TB_CITY.STATE_ID = Convert.ToInt32(ddlState.SelectedValue);
                int cityid = objclsCity.SaveCityMst(objIN_TB_CITY);
                ClearAllControls();
                FillCity();
            }
            else
            {
                clsCity objclsCity = new clsCity();
                IN_TB_CITY objIN_TB_CITY = new IN_TB_CITY();
                objIN_TB_CITY.ID = Convert.ToInt32(Session["CityId"].ToString());
                objIN_TB_CITY.CITY = txtcityname.Text;
                objIN_TB_CITY.STATE_ID = Convert.ToInt32(ddlState.SelectedValue);
                int cityid = objclsCity.SaveCityMst(objIN_TB_CITY);
                FillCity();
                ClearAllControls();
                Session["CityId"] = null;
            }
        }

        catch (Exception)
        {
            throw;
        }
    }

    protected void ClearAllControls()
    {
        try
        {
            txtcityname.Text = string.Empty;
            ddlState.SelectedIndex = 0;
        }
        catch (Exception)
        {

            throw;
        }
    }

    protected void FillCity()
    {
        try
        {
            List<IN_TB_CITY> objIN_TB_CITYist = new List<IN_TB_CITY>();
            clsCity objIN_TB_CITYDAL = new clsCity();
            objIN_TB_CITYist = objIN_TB_CITYDAL.GetAllCities();
            if (objIN_TB_CITYist != null)
            {
                grdCity.DataSource = objIN_TB_CITYist;
                grdCity.DataBind();
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
    protected void grdCity_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Edt")
            {
                int CityId = Convert.ToInt32(e.CommandArgument.ToString());
                clsCity objIN_TB_CITYDAL = new clsCity();
                IN_TB_CITY objIN_TB_CITY = new IN_TB_CITY();
                objIN_TB_CITY = objIN_TB_CITYDAL.GetCityById(CityId);
                if (objIN_TB_CITY != null)
                {
                    ddlState.SelectedValue = objIN_TB_CITY.STATE_ID.ToString();
                    txtcityname.Text = objIN_TB_CITY.CITY;
                    Session["CityId"] = CityId;
                }
            }

            if (e.CommandName == "Del")
            {
                int CityId = Convert.ToInt32(e.CommandArgument.ToString());
                clsCity objIN_TB_CITYDAL = new clsCity();
                IN_TB_CITY objIN_TB_CITY = new IN_TB_CITY();
                objIN_TB_CITYDAL.DeleteCity(CityId);
                FillCity();
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
    protected void grdCity_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Getting State Names
                int StateId = Convert.ToInt32(e.Row.Cells[1].Text);
                clsCity objclsCity = new clsCity();
                IN_TB_STATE objIN_TB_STATE = new IN_TB_STATE();
                objIN_TB_STATE = objclsCity.GetStateById(StateId);
                e.Row.Cells[1].Text = objIN_TB_STATE.STATE;

            }
        }
        catch (Exception)
        {

            throw;
        }
    }
    protected void grdCity_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            grdCity.PageIndex = e.NewPageIndex;
            FillCity();
        }
        catch (Exception)
        {

            throw;
        }
    }
}
